package com.example.init;

import com.example.annotations.Controller;
import com.example.annotations.RequestMapping;
import com.example.auth.MyAuthenticator;
import com.example.auth.MyBasicAuthenticator;
import com.example.handler.RootHandler;
import com.example.handler.StaticHandler;
import com.sun.net.httpserver.HttpServer;
import org.reflections.Reflections;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * 初始化上下文，发布HTTP接口
 */
public class HttpContextInit {

    public static void init(HttpServer server,String packageName){
        server.createContext("/static").setHandler(new StaticHandler());
        Reflections reflections =new Reflections(packageName);
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Controller.class);
        classes.parallelStream().forEach(c->{
            String[] values = null;
            RequestMapping controllerRequestMapping = c.getDeclaredAnnotation(RequestMapping.class);
            if(controllerRequestMapping!=null){
                values = controllerRequestMapping.value();
            }
            if(values != null){
                for(String value:values){
                    Method[] methods = c.getDeclaredMethods();
                    for(Method method:methods){
                        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                        if(requestMapping != null){
                            String[] subValues = requestMapping.value();
                            if(subValues != null){
                                for(String subValue:subValues){
                                    String path = value+subValue;
                                    try {
                                        Object obj = c.getDeclaredConstructor().newInstance();
                                        System.out.println("发布接口：http://"+server.getAddress().getHostName()+":"+server.getAddress().getPort()+path.replaceAll("//","/"));
                                        server.createContext(path.replaceAll("//","/"),new RootHandler(method,obj)).setAuthenticator(new MyAuthenticator());
                                    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
                                             InvocationTargetException e) {
                                        throw new RuntimeException(e);
                                    }
                                }
                            }
                        }
                    }
                }
            }else {
                Method[] methods = c.getDeclaredMethods();
                for(Method method:methods){
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    if(requestMapping != null){
                        String[] subValues = requestMapping.value();
                        if(subValues != null){
                            for(String subValue:subValues){
                                try {
                                    Object obj = c.getDeclaredConstructor().newInstance();
                                    System.out.println("发布接口：http://"+server.getAddress().getHostName()+":"+server.getAddress().getPort()+subValue);
                                    server.createContext(subValue,new RootHandler(method,obj)).setAuthenticator(new MyAuthenticator());
                                } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                                         NoSuchMethodException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                }
            }
        });
    }
}
